<?php
App::uses('AppModel', 'Model');
/**
 * Key Model
 *
 * @property Group $Group
 * @property Role $Role
 */
class Key extends AppModel {
/**
 * Validation rules
 *
 * @var array
 */
	public $validate = array(
		'key' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);

	//The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * belongsTo associations
 *
 * @var array
 */
	public $belongsTo = array(
		'Group' => array(
			'className' => 'Group',
			'foreignKey' => 'group_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'Role' => array(
			'className' => 'Role',
			'foreignKey' => 'role_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);
	
	public function generate($letters = 3, $groups = 3, $seperator = '-') {
		$key = "";
		$alphabet = range('A', 'Z');		
		for($a = 1; $a <= $letters * $groups; $a++) {
			$key .= $alphabet[rand(0, sizeof($alphabet) - 1)];
			if($a % $letters == 0) {
				$key .= $seperator;
			}
		}
		return trim($key, $seperator);
	}
}
